/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package easybooking.clients.gui.report;

import easybooking.ReportClientController;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.UIManager;

/**
 *
 * @author Tim Schröder
 */
public class ReportClientGui {

    private ReportClientController rcc;
    private RCMenuTree reportClientMenuTree;
    private JScrollPane scrollPane;
    private JMenuBar mainMenuBar;
    private JMenu helpJMenu;
    private JMenu fileJMenu;
    private JMenuItem help;
    private JMenuItem about;
    private JMenuItem exit;
    private JPanel mainPanel;
    private JFrame clientMainFrame;
    private JSplitPane splitPane;

    /**
     * creates and shows the gui
     * @param rcc the ReportClientController
     */
    public void createAndShowGUI(ReportClientController rcc) {
        this.rcc = rcc;
        this.createFrame();
        rcc.setServer();
    }

    /**
     * Creates the Frame of the ReportGUI
     */
    private void createFrame() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception exc) {
            System.err.println("Error loading L&F: " + exc);
        }
        clientMainFrame = new JFrame("EasyBooking Report Client");
        clientMainFrame.setDefaultLookAndFeelDecorated(true);
        clientMainFrame.setPreferredSize(new Dimension(1100, 800));
        clientMainFrame.setJMenuBar(this.createMenuBar());
        clientMainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        clientMainFrame.getContentPane().add(createSplitPane());
        clientMainFrame.setResizable(true);
        clientMainFrame.setLocationRelativeTo(null);
        clientMainFrame.pack();
        clientMainFrame.setVisible(true);
        clientMainFrame.setIconImage(new ImageIcon("/easybooking/clients/gui/images/create.png").getImage());
    }

    /**
     * creates a manubar for the ReportGUI
     * @return a JManuBar Object for the ReportGUI
     */
    private JMenuBar createMenuBar() {

        mainMenuBar = new JMenuBar();
        fileJMenu = new JMenu("File");
        helpJMenu = new JMenu("Help");

        help = new JMenuItem("Help");
        about = new JMenuItem("About");
        exit = new JMenuItem("Exit");

        exit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.exit(0);

            }
        });

        about.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                JOptionPane.showMessageDialog(null, "(c) EasyBooking \n Andrea Schaefer \n Bernd Opitz \n Hakan Keles \n Mark Roman Stehle \n Tim Schröder");
            }
        });

        help.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try {
                    InputStream inputStream = this.getClass().getResourceAsStream("/easybooking/clients/resources/manual.pdf");

                    File tempOutputFile = File.createTempFile("manual", "pdf");

                    FileOutputStream out = new FileOutputStream(tempOutputFile);

                    byte buffer[] = new byte[1024];
                    int len;
                    while ((len = inputStream.read(buffer)) > 0) {

                        out.write(buffer, 0, len);

                    }
                    out.close();
                    inputStream.close();

                    Desktop.getDesktop().open(tempOutputFile);

                } catch (IOException ex) {
                    Logger.getLogger(ReportClientGui.class.getName()).log(Level.SEVERE, null, ex);
                }

//                java.net.URL helpURL = FlightServerGui.class.getResource("/easybooking/resources/manual.pdf");
//                try {
//                    Desktop.getDesktop().open(new File(helpURL.toURI()));
//                } catch (URISyntaxException ex) {
//                    Logger.getLogger(FlightServerGui.class.getName()).log(Level.SEVERE, null, ex);
//                } catch (IOException ex) {
//                    Logger.getLogger(FlightServerGui.class.getName()).log(Level.SEVERE, null, ex);
//                }
            }
        });


        helpJMenu.add(help);
        helpJMenu.add(about);
        fileJMenu.add(exit);
        mainMenuBar.add(fileJMenu);
        mainMenuBar.add(helpJMenu);
        return mainMenuBar;
    }

    private JSplitPane createSplitPane() {
        // creates the split pane and adds the tree manu and the main panel
        splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        splitPane.setLeftComponent(this.createTreeMenu());
        splitPane.setRightComponent(this.createStartPanel());
        return splitPane;
    }

    public void setRightPanel(JPanel p) {
        splitPane.setRightComponent(p);
        splitPane.repaint();
        clientMainFrame.getContentPane().repaint();
        clientMainFrame.repaint();
    }

    // Creates the scroll pane and adds the menu tree
    /**
     * Creates the scroll pane and adds the menu tree
     * @return a JScrollPane for the ReportGUI
     */
    private JScrollPane createTreeMenu() {

        // Gets the singelton easyBookingMenuTree object
        reportClientMenuTree = RCMenuTree.ReportClientMenuTree(rcc);

        // Expand all nodes
        reportClientMenuTree.expandAll();

        // Creates the
        scrollPane = new JScrollPane(reportClientMenuTree);
        return scrollPane;
    }

    // Creates the ConfigPanel Form
    /**
     * Creates the ConfigPanel Form
     * @return a JPanal
     */
    private JPanel createStartPanel() {
        mainPanel = new JPanel();

        mainPanel.setBackground(Color.WHITE);
        // Sets the border
        mainPanel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Easybooking Report Client "),
                BorderFactory.createEmptyBorder(20, 20, 20, 20)));

        // Sets the layout of the mainPanel
        mainPanel.setLayout(new GridBagLayout());
        GridBagLayout gbl = new GridBagLayout();

        // Places the GUI elements in the layout using addComponent                                           x, y, w, h, wx, wy
        JLabel lblServerHost = new JLabel();
        Icon StartPic = new ImageIcon(ReportClientGui.class.getResource("/easybooking/clients/gui/images/easyBookingLogo.png"));
        lblServerHost.setIcon(StartPic);
        addComponent(mainPanel, gbl, lblServerHost, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 0, 1, 1, 0, 0);


        return mainPanel;
    }

    // Creates the button panel
    public JPanel getMainPanel() {
        return mainPanel;
    }

    // Adds the elements to the layout
    /**
     * defines the layout of the GUI
     * @param cont container to which the component is added
     * @param gbl GridBagLayout object
     * @param c compontent which is added to the container
     * @param fill how to use free space in the cell around the component
     * @param anchor where to fix an align the component
     * @param x x position in the grid layout
     * @param y y position in the grid layout
     * @param width number of columns the element should spread
     * @param hight number of rows the element should spread
     * @param weightx handels the horizontal free space
     * @param weighty handels the vertical free space
     */
    static void addComponent(Container cont, GridBagLayout gbl, Component c, int fill, int anchor, int x, int y, int width, int hight, double weightx, double weighty) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = fill;
        gbc.anchor = anchor;
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = width;
        gbc.gridheight = hight;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        gbc.insets = new Insets(10, 10, 10, 10);
        //gbl.setConstraints(c, gbc);
        cont.add(c, gbc);


    }
}
